package com.example.jdt_xiazai;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

public class CircleProgressBar extends View {

    private static final String TAG = "CircleProgressBar";
    private float mRingWidth;
    private int mRingColor;
    private int mCircleColor;
    private float mStartAngle;
    private float mSweepAngle;
    private String mText;
    private int mTextColor;
    private float mTextSize;
    private RectF mRectF;
    private Paint mArcPaint;
    private float mCx;
    private float mCy;
    private float mRadius;
    private Paint mCirclePaint;
    private Paint mTextPaint;
    private float mDy;

    public CircleProgressBar(Context context) {
        super(context);
    }


    public CircleProgressBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        //4.在自定义view的构造中获取到设置的属性
        //系统提供的一个功能类,可以从AttributeSet中提取属性
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.CircleProgressBar);
        if (ta !=null) {

            mRingWidth = ta.getDimension(R.styleable.CircleProgressBar_ringWidth, 30);
            mRingColor = ta.getColor(R.styleable.CircleProgressBar_ringColor, 0);
            mCircleColor = ta.getColor(R.styleable.CircleProgressBar_circleColor, 0);
            mStartAngle = ta.getFloat(R.styleable.CircleProgressBar_startAngle, -90);
            mSweepAngle = ta.getFloat(R.styleable.CircleProgressBar_sweepAngle, 0);
            mText = ta.getString(R.styleable.CircleProgressBar_android_text);
            mTextColor = ta.getColor(R.styleable.CircleProgressBar_android_textColor, 0);
            mTextSize = ta.getDimension(R.styleable.CircleProgressBar_android_textSize, 30);

            Log.d(TAG, "text: "+ mText);

            //释放资源
            ta.recycle();
        }

        mArcPaint = new Paint();
        mArcPaint.setColor(mRingColor);
        mArcPaint.setStrokeWidth(mRingWidth);
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStyle(Paint.Style.STROKE);

        mCirclePaint = new Paint();
        mCirclePaint.setColor(mCircleColor);
        mCirclePaint.setAntiAlias(true);

        mTextPaint = new Paint();
        mTextPaint.setTextSize(mTextSize);
        mTextPaint.setColor(mTextColor);
        mTextPaint.setTextAlign(Paint.Align.CENTER);//水平居中


        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
        //文本高度的一半
        float size = (fontMetrics.descent - fontMetrics.ascent) / 2;
        mDy = size - fontMetrics.descent;
    }

    //测量
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        //防止有些人使用的时候给了长方形区域,这里我们强制修改为正方形
        //新的正方形边长
        int max = Math.max(width, height);
        //使用新的边长生成宽高的期望
        int newMeasureSpec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY);

        mRectF = new RectF(0.1f*max, 0.1f*max, 0.9f*max, 0.9f*max);
        mCx = max/2;
        mCy = max/2;
        mRadius = max/4;
        super.onMeasure(newMeasureSpec, newMeasureSpec);
    }

    //5.在绘制的过程中使用获取到的属性
    @Override
    protected void onDraw(Canvas canvas) {
        //1.画环
        canvas.drawArc(mRectF,mStartAngle,mSweepAngle,false,mArcPaint);
        //2.画内圆
        canvas.drawCircle(mCx,mCy,mRadius,mCirclePaint);
        //3.画文本,没有居中,需要处理
        //给定的点不像画圆一样,给定的点和图片左上角对齐,
        //文本给定的点是和文本中一条叫baseline的线对齐的
        canvas.drawText(mText,mCx,mCy+mDy,mTextPaint);
    }

    /**
     *
     * @param progress 进度,0 - 100
     */
    public void setProgress(int progress) {
        mText = progress+" %";
        mSweepAngle = 360f / 100 *progress;

        postInvalidate();
    }

}

